<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

function register_add_page() {
    return drupal_get_form('register_edit_form');
}

function upgrade_user_page() {
    return drupal_get_form('upgrade_user_edit_form');
}
function upgrade_user_result_page($opt) {
    return drupal_get_form('upgrade_user_result_form',$opt);
}

function bind_phone_page(){
    return drupal_get_form('bind_phone_form');
}


function register_edit_form($form, &$form_state) {
    global $user;
    if($user->uid != 0){
        nonghua_common_user_view(null);
//        drupal_goto('account/register');
    }
    $form_state['insert'] = true;
    $form['#user'] =  !empty($user->uid)?user_load($user->uid):drupal_anonymous_user();
    $form_state['build_info']['args'][0] = '';
    $form['account'] = array(
    );
    $form['account']['name'] = array(
        '#type' => 'textfield',
        '#maxlength' => USERNAME_MAX_LENGTH,
        '#required' => TRUE,
        '#attributes' => array('class' => array('username')),
    );


    $form['account']['pass'] = array(
        '#type' => 'password_confirm',
        '#size' => 25,
    );
    $form['account']['email'] = array(
        '#type' => 'textfield',
        '#size' => 32,
        '#maxlength' => 30,
    );

    $form['farmer'] = array(

    );
    $form['farmer']['cell'] = array(
        '#type' => 'textfield',
        '#maxlength' => EMAIL_MAX_LENGTH,
        '#attributes'=>array('id' => 'form-sms-cell')
    );
    $form['farmer']['is_bind'] = array(
        '#type' => 'hidden',
        '#default_value' => '0'
    );

    $form['farmer']['graph_code'] = array(
        '#title' => t('图形验证码'),
        '#default_value' => '',
        '#type' => 'textfield',
        '#maxlength' => 4,
        '#size' => 20,
        '#attributes'=>array('id' => 'form-graph-code', 'src' => '')
    );
    $form['farmer']['refresh_graph_code'] = array(
        '#markup' => '<img style="width:100px; margin:0px 0px 10px 12px;" src="code_gg.php" id="graph_char" onclick="window.refresh_graph_code_function()">',
    );

    $form['farmer']['sms_send'] = array(
        '#markup' => '<a href="javascript:void(0)" onclick = "window.sms_onclick_function()" id="farmer-register-get-sms-code" data-toggle="modal" class="btn btn-mini">获取手机验证码</a><p id="form-sms-span"></p>',
    );
    $form['farmer']['sms_msg'] = array(
        '#markup' => '<p id="farmer-register-get-sms-msg"></p>',
    );
    $form['farmer']['sms_code'] = array(
        '#type' => 'textfield',
        '#title' => '手机验证码',
        '#maxlength' => 6,
    );
    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => '提交',
//        '#validate' => array('register_edit_form_validate'),
        '#submit' => array('register_edit_form_submit'),
    );
    return $form;
}

function register_edit_form_validate($form, &$form_state) {

}

function register_edit_form_submit($form, &$form_state) {
    global $user;
    $id = isset($form_state['build_info']['args'][0]) ? $form_state['build_info']['args'][0] : NULL;
    $address = _get_address_id($form_state);
    $transaction = db_transaction();
    try {
        $role_result = user_role_load_by_name('注册用户');
        $new_user = array(
            'name' => isset($form_state['values']['name']) && !empty($form_state['values']['name']) ? $form_state['values']['name'] : $form_state['values']['cell'],
            'pass' => isset($form_state['values']['pass']) && !empty($form_state['values']['pass']) ? $form_state['values']['pass'] : '123456',
            'mail' => isset($form_state['values']['email']) ? $form_state['values']['email'] : null,
            'signature_format' => 'filtered_html',
            'status' => 1,
            'language' => 'zh-hans',
            'timezone' => 'Asia/Shanghai',
            'init' => isset($form_state['values']['email']) ? $form_state['values']['email'] : null,
            'roles' => array(
                $role_result->rid => '注册用户',
            ),
        );
        $new_user['field_cell']['und'][0]['value'] = isset($form_state['values']['cell']) ? $form_state['values']['cell'] : null;
//        $new_user['field_is_bind']['und'][0]['value'] = isset($form_state['values']['is_bind']) ? $form_state['values']['is_bind'] : 0;
        $account = user_save('', $new_user);
        drupal_goto('user/login');
    }catch (Exception $e){
        $transaction->rollback();
        watchdog_exception('user', $e);
        drupal_set_message(t('操作失败！').$e->getMessage(), 'status', FALSE);
    }
}


function upgrade_user_edit_form($form, &$form_state){

    global $user;
    $account = user_load($user->uid);
    $record = _get_user_info($user->uid);
    $user_info = _get_user_info($user->uid);

    if($user_info['role'] == "升级成农户"){
        if($user_info['status'] =='拒绝成农户' ){
            drupal_goto('account/upgrade/result/0');
        }
        drupal_goto('account/upgrade/result/1');
    }
    if($user_info['role'] == "升级成经销商"){
        if($user_info['status'] =='拒绝成经销商'){
            drupal_goto('account/upgrade/result/0');
        }
        drupal_goto('account/upgrade/result/1');
    }
    if($user_info['role'] == '农户'){
        drupal_goto('farmer/edit/'.$user->uid,['query'=>['farmer_id'=>$user->uid]]);
    }
    if($user_info['role']=='administrator'||$user_info['role'] == '销售员'||$user_info['role'] == '销售管理员'||$user_info['role'] == '农化管理员'||$user_info['role'] == '农化服务员'||$user_info['role'] == '公司领导'){
        drupal_goto('employee/edit/'.$user->uid);
    }

    return $form;
}
function upgrade_user_result_form($form, &$form_state){
    $opt = $form_state['build_info']['args'][0];
    global $user;
   if($opt == '0'){
       $string = '用户申请升级失败！请完善个人基本信息！';
   }else if($opt == '1'){
       $string = '用户已申请升级，请耐心等待。';
   }

    $form['result'] = array(
        '#markup' => $string,
    );
    return $form;
}

function bind_phone_form($form, &$form_state){
    global $user;
    $account = user_load($user->uid);
//    print_r($account);die;
    if(@$account->field_cell['und']['0']['value'] == 1){
        drupal_set_message('已经绑定了手机了哦~', 'status', FALSE);
        drupal_goto('account/upgrade');
    }else{
        $form_state['account'] = $account;
        $form['cell'] = array(
            '#type' => 'textfield',
            '#maxlength' => EMAIL_MAX_LENGTH,
            '#attributes'=>array('id' => 'form-sms-cell')
        );
        $form['is_bind'] = array(
            '#type' => 'hidden',
            '#default_value' => '0'
        );

        $form['graph_code'] = array(
            '#title' => t('图形验证码'),
            '#default_value' => '',
            '#type' => 'textfield',
            '#maxlength' => 4,
            '#size' => 20,
            '#attributes'=>array('id' => 'form-graph-code', 'src' => '')
        );
        $form['refresh_graph_code'] = array(
            '#markup' => '<img style="width:100px; margin:0px 0px 10px 12px;" src="code_gg.php" id="graph_char" onclick="window.refresh_graph_code_function()">',
        );
        $form['sms_send'] = array(
            '#markup' => '<a href="javascript:void(0)" onclick = "window.sms_onclick_function()" id="farmer-register-get-sms-code" data-toggle="modal" class="btn btn-mini">获取手机验证码</a><p id="form-sms-span"></p>',
        );
        $form['sms_msg'] = array(
            '#markup' => '<p id="farmer-register-get-sms-msg"></p>',
        );
        $form['sms_code'] = array(
            '#type' => 'textfield',
            '#title' => '手机验证码',
            '#maxlength' => 6,
        );
        $form['submit'] = array(
            '#type' => 'submit',
            '#value' => '绑定',
            '#submit' => array('bind_phone_form_submit'),
        );
    }
    return $form;
}

function bind_phone_form_submit($form, &$form_state){
    global $user;
    $id = isset($form_state['build_info']['args'][0]) ? $form_state['build_info']['args'][0] : NULL;
    $address = _get_address_id($form_state);
    $transaction = db_transaction();
    try {
        $new_user = array(
//            'name' => isset($form_state['values']['name']) && !empty($form_state['values']['name']) ? $form_state['values']['name'] : $form_state['values']['cell'],
//            'pass' => isset($form_state['values']['pass']) && !empty($form_state['values']['pass']) ? $form_state['values']['pass'] : '123456',
//            'mail' => isset($form_state['values']['email']) ? $form_state['values']['email'] : null,
            'signature_format' => 'filtered_html',
            'status' => 1,
            'language' => 'zh-hans',
            'timezone' => 'Asia/Shanghai',
        );
        $new_user['field_cell']['und'][0]['value'] = isset($form_state['values']['cell']) ? $form_state['values']['cell'] : null;
        $new_user['field_is_bind']['und'][0]['value'] = isset($form_state['values']['is_bind']) ? $form_state['values']['is_bind'] : 0;
        $account = $form_state['account'];
        $account = user_save($account, $new_user);
    }catch (Exception $e){
        $transaction->rollback();
        watchdog_exception('user', $e);
        drupal_set_message(t('操作失败！').$e->getMessage(), 'status', FALSE);
    }
}
